<?php

namespace App\Http\Controllers;

use App\Models\Notice;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\NoticeRequest;
use Auth;
class NoticesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function index()
	{
		$notices = Notice::recent()->paginate();
        $user = Auth::user();
		return view('notices.index', compact('notices', 'user'));
	}

	public function create(Notice $notice)
	{
		$user = Auth::user();
    $this->authorize('destroy', $notice);
    return view('notices.create_and_edit', compact('notice', 'user'));
	}

	public function store(Notice $notice, NoticeRequest $request)
	{
        $this->authorize('destroy', $notice);
        $notice = Notice::create($request->all());
		return redirect()->route('notices.index')->with('message', '成功添加一条通知.');
	}

	public function destroy(Notice $notice)
	{
		$this->authorize('destroy', $notice);
		$notice->delete();
		return redirect()->route('notices.index')->with('message', 'Deleted successfully.');
	}
}
